package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import entity.EasyTextArea;
import logic.DuplicateRemoval;
import logic.FileChooser;
import logic.FilesClassify;
import logic.Rename;

/**
 * 文件批量重命名.
 * @author AL
 * 
 */
public class Door {
 
	private JFrame window;
	private JButton choose, rename, classify, removal;
	private JTextField input;
	private volatile EasyTextArea msg;
	private JPanel panel;
	private JScrollPane scroll;
	private volatile JScrollBar bar;// 滚动条
	private Font bigFont, smallFont;

	/**
	 * 用于接收用户选择的文件夹.
	 */
	public static File dir;
	
	/**
	 * 用于标记程序当前是否正在运行，避免用户重复点击.
	 */
	public static volatile boolean running = false;

	public static void main(String[] args) {
		new Door();
	}

	private Door() {
		// 初始化变量（及必要的类）
		bigFont = new Font("Microsoft yahei", Font.PLAIN, 20);
		smallFont = new Font("Microsoft yahei", Font.PLAIN, 13);
		window = new JFrame("批量重命名工具");
		choose = new JButton("选择路径");
		rename = new JButton("重命名");
		classify = new JButton("归档");
		removal = new JButton("去重");
		input = new JTextField("请输入要插入的关键字");
		msg = new EasyTextArea();
		panel = new JPanel();
		scroll = new JScrollPane(msg);
		bar = scroll.getVerticalScrollBar();

		// 设置窗口元素样式
		msg.setFont(bigFont);
		msg.setEditable(false);
		choose.setFont(smallFont);
		rename.setFont(smallFont);
		classify.setFont(smallFont);
		removal.setFont(smallFont);
		msg.setForeground(Color.GRAY);
		input.setFont(smallFont);
		input.setForeground(Color.RED);

		// 设置布局
		Container cp = window.getContentPane();
		panel.setLayout(new GridLayout(1, 4));

		// 按照布局添加元素
		panel.add(choose);
		panel.add(rename);
		panel.add(classify);
		panel.add(removal);
		cp.add(panel, BorderLayout.NORTH);
		cp.add(input, BorderLayout.SOUTH);
		cp.add(scroll, BorderLayout.CENTER);

		// 绑定鼠标监视器
		choose.addMouseListener(new ButtonAction(choose));
		rename.addMouseListener(new ButtonAction(rename));
		classify.addMouseListener(new ButtonAction(classify));
		removal.addMouseListener(new ButtonAction(removal));
		input.addMouseListener(new InputAction(input));

		// 监听鼠标右键
		msg.addMouseListener(new RightButtonMenu(msg, smallFont));

		// 绑定鼠标单击事件
		choose.addActionListener(e -> dir = FileChooser.action(msg));
		rename.addActionListener(e -> Rename.action(input, msg, bar));
		classify.addActionListener(e -> FilesClassify.action(msg, bar));
		removal.addActionListener(e -> DuplicateRemoval.action(msg, bar));

		// 设置窗口属性
		window.setSize(500, 300);
		window.setResizable(false);
		window.setVisible(true);
		window.setLocationRelativeTo(null);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
}